package Servlets.yz;

import Bean.User;
import Bean.supplier;
import Util.GokJDBCUtil2;
import Util.SlatMD5Util;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
/*
* 用于更新用户信息功能
* */
@WebServlet(name = "UserUpdateServlet",urlPatterns = "/UserUpdateServlet")
public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");

        if("updateBefore".equals(method)){//更新之前，获取原供应商信息
            String id = req.getParameter("id");
            String sql = "select * from user where id=?";
            User user = GokJDBCUtil2.queryForObject(sql, User.class, id);
            req.setAttribute("user",user);
            req.getRequestDispatcher("pages/userUpdate.jsp").forward(req,resp);
        }
        else if("update".equals(method)){//结合更新之间的数据，进行相应更新
            int id = Integer.parseInt(req.getParameter("userID"));//用户ID
            String userName = req.getParameter("userName");//用户姓名
            String userPassword = req.getParameter("userPassword");//用户密码
            String password = SlatMD5Util.generateSaltPassword(userPassword);
            String nickName = req.getParameter("nickName");//用户昵称
            String phone = req.getParameter("phone");//用户电话
            String email = req.getParameter("email");//用户电子邮箱
            String address = req.getParameter("address");//用户地址
            String sql = "update  user set username=?,userpassword=?,usernickname=?,userphone=?,useremail=?,useraddress=? where id=?";
            GokJDBCUtil2.update(sql,userName,password,nickName,phone,email,address,id);
            resp.sendRedirect(req.getContextPath()+"/pages/userUpSuccess.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
